package com.yd.app.service.pc.messageService;

import com.yd.app.mapper.MessageMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.business.Message;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.MessageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-13 9:47
 * @desc
 **/
@Service
public class MessageService extends BaseService{
    @Autowired
    private MessageMapper messagemapper;
    /**
     * @Author: zy
     * @Desc: 录入商品类型
     * @Date:  2018/3/13
     * @Param
     */
    public void  addMessage(Message message){
        messagemapper.addMessage(message);
    }
    /**
     * @Author: zy
     * @Desc: 商品类型修改
     * @Date:  2018/3/13
     * @Param
     */
    public void updateMessage(Message message){
        messagemapper.updateMessage(message);
    }
    /**
     * @Author: zy
     * @Desc: 商品类型删除
     * @Date:  2018/3/13
     * @Param
     */
    public void deleteMessage(String id){
        messagemapper.deleteMessage(id);
    }
    /**
     * @Author: zy
     * @Desc:  分页查询商品类型
     * @Date:  2018/3/13
     * @Param
     */
    public PageModel getMessageList(MessageParam messageParam, int pageIndex, int pageSize){
        PageModel pm=new PageModel();
        List<Message> list=new ArrayList<>();
        list=messagemapper.searchMessageList(messageParam,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messagemapper.searchMessageCount(messageParam);
            pm.setCount(count);
        }
        pm.setList(list);
        return pm;
    }
    public PageModel getMessageTalkForList(String userId,String loginTserId ,int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Message> list=new ArrayList<>();
        String token=getToken(userId,loginTserId);
        list=messagemapper.searchMessageTalkList(token,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=messagemapper.searchMessageTalkCount(token);
            pm.setCount(count);
        }
       /* Collections.sort(list, new Comparator<Message>() {
            @Override
            public int compare(Message o1, Message o2) {
                if(o1.getPublishTime().getTime()-o2.getPublishTime().getTime()>0){
                    return 1;
                }else if(o1.getPublishTime().getTime()-o2.getPublishTime().getTime()<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });*/
        pm.setList(list);
        return pm;
    }
    //更新已读状态
    public void updateMessageIfRead(String userId,String loginUserId,boolean b) {
        messagemapper.updateMessageIfRead(loginUserId,getToken(userId,loginUserId),b);
    }

    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/13
     * @Param
     */
    public void deleteAll(Message message){
        messagemapper.deleteAll(message);
    }

    public void cleanMessage(String userId, String userId1) {
        messagemapper.cleanMessage(getToken(userId,userId1));
    }
}
